package com.zby.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.admin.mapper.ReturnReasonMapper;
import com.zby.admin.pojo.ReturnReason;
import com.zby.admin.pojo.dto.ReturnReasonDto;
import com.zby.admin.pojo.vo.ReturnReasonVo;
import com.zby.admin.service.ReturnReasonService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReturnReasonServiceImp extends ServiceImpl<ReturnReasonMapper, ReturnReason> implements ReturnReasonService {
    @Resource
    private ReturnReasonMapper returnReasonMapper;
    
    @Override
    public boolean add(ReturnReasonDto returnReasonDto) {
        ReturnReason returnReason = new ReturnReason();
        returnReason.setReason(returnReasonDto.getReason());
        returnReason.setDescription(returnReasonDto.getDescription());
        return  returnReasonMapper.insert(returnReason)>0;
    }
    
    @Override
    public Page<ReturnReasonVo> getAllReasonList(Integer page, Integer pageSize, String reasonTitle) {
        // 创建分页对象
        Page<ReturnReason> returnReasonPage = new Page<>(page, pageSize);
        
        // 创建查询条件
        LambdaQueryWrapper<ReturnReason> queryWrapper = new LambdaQueryWrapper<>();
        if (reasonTitle != null && !reasonTitle.isEmpty()) {
            queryWrapper.like(ReturnReason::getReason, reasonTitle);
        }
        
        // 执行分页查询
        Page<ReturnReason> resultPage = this.page(returnReasonPage, queryWrapper);
        
        // 转换为VO对象
        Page<ReturnReasonVo> voPage = new Page<>(resultPage.getCurrent(), resultPage.getSize(), resultPage.getTotal());
        List<ReturnReasonVo> voList = resultPage.getRecords().stream().map(returnReason -> {
            ReturnReasonVo vo = new ReturnReasonVo();
            BeanUtils.copyProperties(returnReason, vo);
            vo.setCreateTime(returnReason.getCreateTime().toLocalDate());
            return vo;
        }).collect(Collectors.toList());
        
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public List<ReturnReasonVo> selectList() {
        List<ReturnReason> returnReasonList = this.list();
        if (returnReasonList != null && !returnReasonList.isEmpty()) {
            return returnReasonList.stream().map(returnReason -> {
                ReturnReasonVo vo = new ReturnReasonVo();
                BeanUtils.copyProperties(returnReason, vo);
                vo.setCreateTime(returnReason.getCreateTime().toLocalDate());
                return vo;
            }).collect(Collectors.toList());
        }
        return List.of();
    }
}